﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IteratorClass
{
    public class IteratorCollection : IEnumerable
    {
        public int Name { get; set; }

        public int Age { get; set; }

        protected int[] intArr = null;

        protected int curIndex = -1;


        public IteratorCollection(int caption)
        {
            intArr = new int[caption];
        }


        public bool Add(int nu)
        {
            if (curIndex >= intArr.Length - 1)
            {
                return false;
            }
            curIndex++;
            intArr[curIndex] = nu;
            return true;
        }

        public bool Remove(int index)
        {
            if (index>curIndex)
            {
                return false;
            }
            Array.Copy(intArr, index+1, intArr, index , curIndex - index);
            curIndex--;
            return true;
        }

        public bool RemoveEnd()
        {
            if (curIndex<0)
            {
                return false;
            }
            this.curIndex--;
            return true;
        }

        public bool RemoveHead()
        {

            return Remove(0);
        }

        public IEnumerator GetEnumerator()
        {
            return new MyIterator(this);

            //for (int i = 0; i < curIndex; i++)
            //{
            //    yield return this.intArr[i];
            //}
        }

        private class MyIterator : IEnumerator
        {
            IteratorCollection collection;
            private int index = -1;
            public object Current => collection.intArr[index];

            public MyIterator(IteratorCollection col)
            {
                this.collection = col;
                index = -1;
            }

            public bool MoveNext()
            {
                index++;
                return (index <= collection.curIndex);
            }

            public void Reset()
            {
                index = -1;
            }
        }
    }


}
